﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using TrainingsApp.Model;
using TrainingsApp.Logic;
using ZJFramework10.Extension;
using TrainingsApp.WebModel.Route;
namespace TrainingsApp.IPADApp.UserControls.Main.Student {
    public partial class UC_Register_Detail : ControlBase {
        //protected override void OnInit(EventArgs e) {
        //    TemplateUserInfoManager.Push(new TS_User_Entity {
        //        UserName = "Test" + new Random().Next(1000, 9000),
        //        UserType = (int)UserType.Children,
        //    });
        //    base.OnInit(e);
        //}
        protected void Page_Load(object sender, EventArgs e) {
            Initialize();
        }


        protected void Initialize() {

            this.dropYear.DataSource = StaticDataSource.ListYear();
            this.dropYear.DataTextField = "Text";
            this.dropYear.DataValueField = "Value";
            this.dropYear.DataBind();

            this.dropMonth.DataSource = StaticDataSource.ListMonth();
            this.dropMonth.DataTextField = "Text";
            this.dropMonth.DataValueField = "Value";
            this.dropMonth.DataBind();

            this.dropDay.DataSource = StaticDataSource.ListDay();
            this.dropDay.DataTextField = "Text";
            this.dropDay.DataValueField = "Value";
            this.dropDay.DataBind();

            this.dropRef.DataSource = StaticDataSource.ListPatriarchType();
            this.dropRef.DataTextField = "Text";
            this.dropRef.DataValueField = "Value";
            this.dropRef.DataBind();

            dropProvince.DataSource = TrainingsApp.Logic.TS_Province_Logic.TS_Province_ListByPaging(1000, 1).TS_Province_List;
            dropProvince.DataTextField = "ProvinceName";
            dropProvince.DataValueField = "Idx";
            dropProvince.DataBind();

        }

        protected void btnSubmit_Click(object sender, EventArgs args) {
            int classId = ZJFramework10.Utility.FormManager.GetValue<int>("dropClass", 0);
            int schoolId = ZJFramework10.Utility.FormManager.GetValue<int>("dropSchool", 0);


            TS_User_Entity userEntity = null;
            TemplateUserInfoManager.TryGet(out userEntity);

            TS_User_Children_Entity children = new TS_User_Children_Entity();
            children.ClassesId = classId;

            children.BirthDay = new DateTime(this.dropYear.SelectedValue.AsInt(), this.dropMonth.SelectedValue.AsInt(), this.dropDay.SelectedValue.AsInt());

            children.Email = userEntity.Email;
            children.Gender = userEntity.Gender;
            children.Grade = classId;
            children.IdentityCard = userEntity.IdentityCard;
            children.Mobile = userEntity.Mobile;
            children.Name = this.txtName.Text;
            children.Password = userEntity.Password;
            children.PhotoImage = string.Empty;
            children.SchoolId = schoolId;
            children.StuCode = string.Empty;
            children.UserName = userEntity.UserName;
            children.UserType = userEntity.UserType;
            children.UserSource = UserSources.Register;
            TS_User_Patriarch_Entity parent = new TS_User_Patriarch_Entity();
            parent.UserName = userEntity.UserName + "_Patriarch";
            parent.Password = userEntity.Password;
            parent.Name = this.txtParentName.Text;
            parent.WorkOfCompany = this.txtCompany.Text;
            parent.Position = this.txtPosition.Text;
            parent.UserType = (int)UserType.Parent;
            parent.Refrence = (byte)this.dropRef.SelectedValue.AsInt();
            parent.UserSource = UserSources.Register;
            children = TrainingsApp.Logic.TS_User_Children_Logic.TS_User_Children_Add(children, parent);
            if (children.UserId > 0) {

                var user = Sys_Permission_Logic.GetUserInfo(children.UserName);

                Session["CurContext"] = user;
                TemplateUserInfoManager.Clear();
                Response.Redirect(this.CurPage.RouteHelper.Route(RouteNames.Success, new { code = SuccessCode.RegisterSuccess }));
            }
        }
    }
}